//1. 导入 axios 
import axios from 'axios';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import {toast} from 'react-toastify'

//2.  新建一个 axios 对象 设置 axios 的默认配置
let axiosObj = axios.create({
    baseURL : 'http://127.0.0.1:3001'
});

//3. 请求进度条的添加          配置对象 {method, url, headers,data,timeout, baseURL}
axiosObj.interceptors.request.use(config => {
    NProgress.start();
    return config;
});

axiosObj.interceptors.response.use(response => {
    NProgress.done();
    return response.data;
}, error => {
    toast.error("网络异常, 请稍后再试...", {
        position: toast.POSITION.TOP_CENTER
    });
    //返回
    // return Promise.reject(error);
    //返回一个 pending 状态的 promise 对象
    return new Promise(() => {});
});

//暴露 axios
export default axiosObj;